module.exports = app => {
    let store = app.getStore();
    let mongo = app.mongo;
    return class OrderController extends app.Controller {
        async add() {
            // 验证是否已经登录
            const {token} = {
                ...this.ctx.request.body,
                ...this.ctx.query
            }
            const user = store.get({token});
            if (!user) {
                this.ctx.body = {
                    code: 1,
                    msg: '请先登录'
                }
                return false;
            } 
            if (user.status !== 0) {
                this.ctx.body = {
                    code: 1,
                    msg: '会话已过期'
                }
                return false;
            }
            this.ctx.body = '添加订单';
        }
        
        async createOrder() {
            const res = await this.ctx.service.order.createOrder();
            if (res.code === 0) {
                this.ctx.body = res;
            } else {
                this.ctx.body = {
                    code: 1,
                    msg: 'success'
                }
            }
        } 
        
        async delOrder() {
            const res = await this.ctx.service.order.delOrder();
            if (res.code === 0) {
                this.ctx.body = res;
            } else {
                this.ctx.body = {
                    code: 1,
                    msg: 'success'
                }
            }
        }
        
        async orderList() {
            const res = await this.ctx.service.order.orderList();
            if (res.code === 0) {
                this.ctx.body = res;
            } else {
                this.ctx.body = {
                    code: 1,
                    msg: 'fail'
                }
            }
        }
    }
}